/**
 *
 */
;
//存储报警信息，判断目前是否还存在报警，来确定是否播放警铃
var alarmMap = new Map();



//状态转换
function getStatusStr(status){
    var statusStr='normal';
    switch (status) {
        case 1:
            statusStr = "fire";  //火警
            break;
        case 2:
            statusStr = "normal"; //静音
            break;
        case 3:
            statusStr = "malfunction"; //故障
            break;
        case 4:
            statusStr = "lowpower"; //低压
            break;
        case 7:
            statusStr = "normal"; //正常待机
            break;
        case 9:
            statusStr = "normal"; //自检数据上报
            break;
        default:
            break;
    }
    return statusStr;
}

//设置设备总数
function getDeviceSum(){
    var devCount = 0;
    $.ajax({
        url:"/device/count",
        type:"GET",
        async:false,
        success:function(result){
            if(result.status=="200"){
                devCount = result.data;

            } else {

            }
        }

    })
    $(".sumSmoke").text(devCount);
}
//点聚合方案
function polymerizationLayoutIni(){
    //初始化地图，获取当前组织经纬度
    BaiDuMap.initialize('120.182701','30.223314',20);

    //获取设备列表，并在地图上画出
//      $.ajax({
//        url:"/device/data/list",
//        type:"GET",
//        async:false,
//        success:function(result){
//          if(result.status=="200"){
//            for(var ri in result.data) {
//
//              var r = result.data[ri];
//              BaiDuMap.addDot(r.latitude,r.longitude,r.devCode,r.deviceName,r.address,'normal',r.linkMain1,r.linkPhone1,r.linkMain2,r.linkPhone2,r.linkMain3,r.linkPhone3,r.linkMain4,r.linkPhone4);
//            
//            }
//
//          } else {
//
//          }
//        }
//
//      })

    //获取设备状态列表，并在地图上更新  (聚合模式)
    $.ajax({
        url:"/deviceStatus/data/list",
        type:"GET",
        success:function(result){
            if(result.status=="200"){
                var markers = [];
                var marker = null;
                var i = 0;
                
                for (; i < result.data.length; i++) {
                    var r = result.data[i];
                    
                  //start
                                      var deviceName = r.device.deviceName;
                                      var address = r.device.address;
                                      var linkMain1 = r.device.linkMain1;
                                      var linkMain2 = r.device.linkMain2;
                                      var linkMain3 = r.device.linkMain3;
                                      var linkMain4 = r.device.linkMain4;
                                      var linkPhone1 = r.device.linkPhone1;
                                      var linkPhone2 = r.device.linkPhone2;
                                      var linkPhone3 = r.device.linkPhone3;
                                      var linkPhone4 = r.device.linkPhone4;
                                      var devCode = r.devCode;
                    
                    if(r.status == 1){
                        alarmMap.set(r.devCode,r.status);
                    }
                    var point = new BMap.Point(r.device.latitude,r.device.longitude);

//                    if(r.status == 1){
//                        alarmMap.set(r.devCode,r.status);
//                        var myIcon = new BMap.Icon("map/images/fire.png", new BMap.Size(32,37));
////        				var marker2 = new ComplexCustomOverlay(point);
////                        marker2.devCode = devCode;		
////        				map.addOverlay(marker2);
//                        
//                    }else if(r.status == 4){
//                        var myIcon = new BMap.Icon("map/images/lowpower.png", new BMap.Size(32,37));
//                    }else if(r.status == 3){
//                        var myIcon = new BMap.Icon("map/images/malfunction.png", new BMap.Size(32,37));
//
//                    }else if(r.status == 7){
//                        var myIcon = new BMap.Icon("map/images/normal.png", new BMap.Size(32,37));
//
//                        
//                    }
//
//                    var marker = new BMap.Marker(point,{icon:myIcon});
                    sContentHtml (point,r.status,marker,markers,deviceName,address,linkMain1,linkMain2,linkMain3,linkMain4,linkPhone1,linkPhone2,linkPhone3,linkPhone4,devCode);
                    
                    //清除覆盖物
                    
                    //map.clearOverlays();
//                    if(r.status == 7){
//                    	console.log(22)
//                    	map.clearOverlays();
//                    }
                    

                }
                //最简单的用法，生成一个marker数组，然后调用markerClusterer类即可。

                var markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});
                map.clearOverlays();
                


                $.each(result.data,function (index,item) {
                	if(item.status == 7) {
                		
                		//BaiDuMap.deletePoint(this.device.devCode);
                	}
                })
                
//                markerClusterer.removeMarkers(result.data)
//                result.data.splice(0,result.data.length)
//                result.data(data)
                
                
                
                
               // BaiDuMap.deletePoint(devCode);
                
                

                
            } else {

            }
        }
    })

    
    
                  $(".faultMenu").on("click",function () {
            	map.clearOverlays();


            	
            	
            	
            });
    
    
    
    
    //建立websocket
    var websocket = null;

    //鍒ゆ柇褰撳墠娴忚鍣ㄦ槸鍚︽敮鎸乄ebSocket
    if('WebSocket' in window){
        var webhost = location.hostname;
        websocket = new WebSocket("ws://"+webhost+":9090/websocket");
//           websocket = new WebSocket("ws://47.96.179.13:9090/websocket");
    }
    else{
    	winTip.alert('Not support websocket')
    }

    //杩炴帴鍙戠敓閿欒鐨勫洖璋冩柟娉?
    websocket.onerror = function(){
        setMessageInnerHTML("error");
    };

    //杩炴帴鎴愬姛寤虹珛鐨勫洖璋冩柟娉?
    websocket.onopen = function(event){
        setMessageInnerHTML("open");
    }

    //鎺ユ敹鍒版秷鎭殑鍥炶皟鏂规硶
    websocket.onmessage = function(event){
        //setMessageInnerHTML(event.data);
        var str = event.data;
        var strs = str.split(",");
        if(strs.length>=8){
            var devCode=strs[0];
            var status=strs[1];
            var statusStr =strs[2] ;
            var Latitude = strs[3];
            var Longitude = strs[4];
            var deviceName = strs[5];
            var address = strs[6];
            var returntime=strs[7];
            var linkMain1=strs[8];
            var linkPhone1=strs[9];
            var linkMain2=strs[10];
            var linkPhone2=strs[11];
            var linkMain3=strs[12];
            var linkPhone3=strs[13];
            var linkMain4=strs[14];
            var linkPhone4=strs[15];

            if(status==1){
                alarmMap.set(devCode,status);
                playSound();
                speach('警报：'+address+deviceName+'烟雾报警');
            } else {
                alarmMap.delete(devCode);
                if(alarmMap.size==0){
                    stopSound();
                    hideWarning();
                }
            }
            BaiDuMap.deletePoint(devCode);
            BaiDuMap.addDot(Latitude,Longitude,devCode,deviceName,address,statusStr,linkMain1,linkPhone1,linkMain2,linkPhone2,linkMain3,linkPhone3,linkMain4,linkPhone4);
            if(status==1){
                //定位图标
                BaiDuMap.panTo(Latitude,Longitude);

                //设置报警窗口信息
                setAlertWarningInfo(deviceName,address,linkMain1,linkPhone1,devCode,returntime);
                //弹出报警窗口
                alertWarning();
            }


            alarmInfo(deviceName,address,returntime,Latitude,Longitude,devCode,status);
            if(status!=7){
                historyInfo(deviceName, address, returntime, Latitude, Longitude, devCode, status);
            }
        }
    }

    //杩炴帴鍏抽棴鐨勫洖璋冩柟娉?
    websocket.onclose = function(){
        setMessageInnerHTML("close");
    }

    //鐩戝惉绐楀彛鍏抽棴浜嬩欢锛屽綋绐楀彛鍏抽棴鏃讹紝涓诲姩鍘诲叧闂瓀ebsocket杩炴帴锛岄槻姝㈣繛鎺ヨ繕娌℃柇寮?灏卞叧闂獥鍙ｏ紝server绔細鎶涘紓甯搞??
    window.onbeforeunload = function(){
        websocket.close();
    }

    //灏嗘秷鎭樉绀哄湪缃戦〉涓?
    function setMessageInnerHTML(innerHTML){
        // document.getElementById('message').innerHTML += innerHTML + '<br/>';
    }

    //鍏抽棴杩炴帴
    function closeWebSocket(){
        websocket.close();
    }

    //鍙戦?佹秷鎭?
    function send(){
        websocket.send(message);
    }
}


// 自定义覆盖物(添加报警光圈)
function ComplexCustomOverlay(point){
    this._point = point;
//    this._no = devCode;
//    this.devCode = null;
}
ComplexCustomOverlay.prototype = new BMap.Overlay();
ComplexCustomOverlay.prototype.initialize = function(map){
    this._map = map;
    var div = this._div = document.createElement("div");
    div.className="active-alarm-pointer";
    div.style.position = "absolute";
    div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);		        
    var activeStyleDiv = "<div class='dot'></div>" +
                         "<div class='pulse'></div>" +
                         "<div class='pulse1'></div>";
    div.innerHTML=activeStyleDiv;

    div.onmouseover = function(){
    	//
    }

    div.onmouseout = function(){
    	//
    }

    map.getPanes().labelPane.appendChild(div);
    div.parentElement.style.zIndex=div.style.zIndex+1;//Let ripple be below the icon
    return div;
}
ComplexCustomOverlay.prototype.draw = function(){
    var map = this._map;
    var pixel = map.pointToOverlayPixel(this._point);
    this._div.style.left = pixel.x -40-8+'px';
	this._div.style.top = pixel.y -40+10+'px';
}

ComplexCustomOverlay.prototype.getPosition = function () {
    return this._point;
}




function sContentHtml (point,status,marker,markers,deviceName,address,linkMain1,linkMain2,linkMain3,linkMain4,linkPhone1,linkPhone2,linkPhone3,linkPhone4,devCode) {
   
    if(status == 1){
        alarmMap.set(devCode,status);
        var myIcon = new BMap.Icon("map/images/fire.png", new BMap.Size(32,37));
//		var marker2 = new ComplexCustomOverlay(point);
//        marker2.devCode = devCode;		
//		map.addOverlay(marker2);
        
    }else if(status == 4){
        var myIcon = new BMap.Icon("map/images/lowpower.png", new BMap.Size(32,37));
    }else if(status == 3){
        var myIcon = new BMap.Icon("map/images/malfunction.png", new BMap.Size(32,37));

    }else if(status == 7){
        var myIcon = new BMap.Icon("map/images/normal.png", new BMap.Size(32,37));

        
    }

    var marker = new BMap.Marker(point,{icon:myIcon});
	
	
	
	
	
	
	
	
	//弹出详细信息窗口

    var sContent = "<div style='margin-top: -20px;'>"
    sContent += "<h3><div style='display: inline-block;'><strong style='font-size: 16px;'>设备名称:</strong><strong style='font-size: 16px;color:#f00;'>"+deviceName+"</strong></div></h3><hr style='margin-top: 0px;margin-bottom: 0px;'>";
    sContent += "<div style='padding: 4px;color: #666;min-width: 120px;'><div><table class='baidumap-tip-table'><tbody><tr><td class='tit' ><i class='fa fa-map-marker'></i>地址：</td><td colspan='2'>"+address+"</td></tr>";
    sContent += "<tr><td class='tit'><i class='fa fa-user-md'></i>联系人:</td><td class='col2-wid'>";
    if((linkMain1!=null)&&(linkMain1.length!=0)){
        sContent += linkMain1+"</td><td>"+linkPhone1+"</td> </tr>";
    }
    if((linkMain2!=null)&&(linkMain2.length!=0)){
        sContent += "<tr><td class='tit'></td><td class='col2-wid'>"+linkMain2+"</td><td>"+linkPhone2+"</td> </tr>";
    }
    if((linkMain3!=null)&&(linkMain3.length!=0)){
        sContent += "<tr><td class='tit'></td><td class='col2-wid'>"+linkMain3+"</td><td>"+linkPhone3+"</td> </tr>";
    }
    if((linkMain4!=null)&&(linkMain4.length!=0)){
        sContent += "<tr><td class='tit'></td><td class='col2-wid'>"+linkMain4+"</td><td>"+linkPhone4+"</td> </tr>";
    }
    sContent += "<tr><td class='tit'><i class='fa  fa-cube'></i>设备号:</td><td colspan='2'>"+devCode+"</td></tr>";
    sContent +=	"</tbody></table></div></div>";
    sContent += "</div>";
    
    marker.devCode = devCode;
    
    var opts = {
//			width : 250, // 信息窗口宽度
//			height: 180, // 信息窗口高度
//			title : "信息窗口" , // 信息窗口标题
        enableMessage:true//设置允许信息窗发送短息
    };

    BaiDuMap.addClickHandler(sContent, marker);
    markers.push(marker);

}


























